import requests
from requests.api import head
import time
import re
import execjs
import urllib

def get_session():
    t = int(time.time()*1000)
    url = 'https://www.sf-express.com/sf-service-core-web/service/user/qrcode/getqrcode?timestamp={}'.format(t)
    headers1 = {
        'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Host': 'www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/dynamic_function/waybill/',
        'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'Sec-Fetch-Dest': 'image',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
    }
    response = requests.get(url,headers=headers1)
    session = response.headers['Set-Cookie']
    session = re.findall(r'SESSION=(.*?);',session)[0]
    print(session)

def get_code():
    t = int(time.time()*1000)
    url = 'https://www.sf-express.com/sf-service-owf-web/service/captcha/sms?force=true&time={}'.format(t)
    res = requests.get(url)
    with open('sf_code.png','wb') as f:
        f.write(res.content)

def get_yzm(phone,code):
    url = 'https://www.sf-express.com/sf-service-owf-web/service/sms/user/{}/notification?app=sms&answer={}&area=&lang=sc&region=cn&translate='.format(phone,code)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
    }

def get_data(session):
    url = 'https://www.sf-express.com/sf-service-core-web/service/waybills/list/rec?lang=sc&region=cn&translate=&pageNo=1&pageRows=10'
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
    }

    Passwd = execjs.compile(open(r"E:/nodeProject/js/uuid.js",encoding='UTF-8').read()).call('UUID')
    print(Passwd)
    cookie = 'sajssdk_2015_cross_new_user=1;'+'sensorsdata2015jssdkcross={"distinct_id":"'+Passwd+'","first_id":"","props":{},"$device_id":"'+Passwd+'"}'
    cookie = urllib.parse.quote(cookie)
    cookie = cookie + ';SESSION='+session+';' + 'loginUser=18718896370;'
    # + 'loginUser=18718896370;' + 'sajssdk_2015_cross_new_user=1;'
    cookies = {
        'Cookie': cookie
    }
    # cookies = {
    #     'Cookie':'sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross={"distinct_id":"17a6602b4cf558-045d7d6971c1de-c3f3568-2073600-17a6602b4d0393","first_id":"","props":{},"$device_id":"17a6602b4cf558-045d7d6971c1de-c3f3568-2073600-17a6602b4d0393"}; SESSION=e95e3982a6604f5a8c1da876b5b03636'
    # }
    response = requests.get(url,headers=headers,cookies=cookies)
    print(response.json())

#---------------------------------------------------------------------
#第一次测试时间  三点整      正常运行
#第二次测试时间  三点二十分  正常运行
#第三次测试时间  三点四十分  正常运行
#第四次测试时间  四点零六分  正常运行
#第五次测试时间  四点三十分  正常运行
#第六次测试时间  五点整      正常运行
#第七次测试时间  五点二十分  正常运行
#第八次测试时间  六点整      失效